package gui;

import checker.DicoABR;
import checker.Checker;
import checker.Dictionary;
import checker.DictionarySet;

import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import jsc.base.JSCerror;
import jsc.base.WordEnumerator;

public class MainFrame extends JFrame {

	private DictionarySet dictionaries;
	private static final long serialVersionUID = 1L;
	private JTextArea textfield = new JTextArea(30, 60);

	public MainFrame(Checker corrector) {
		super("Java Spell Checker");
		this.makeFrame();
		this.dictionaries = new DictionarySet(new DicoABR(
				Dictionary.DEFAULT_DICTIONARY));
	}

	public MainFrame() {
		this(null);
	}

	private void makeFrame() {

		Container saisiePane = getContentPane();

		FlowLayout layout = new FlowLayout();
		Dimension dimensions = new Dimension(700, 600);

		final MainFrame m = this;
		JButton checkSpell = new JButton("Spell Check");
		checkSpell.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String text = textfield.getText();
					Checker c = new Checker(dictionaries, text);
					c.check();
					WordEnumerator parser = new WordEnumerator(text);
					while (!parser.noMoreWord()) {
						String word = parser.getWord();
						if (c.isAnError(word)) {
							new SuggestionFrame(c.getErrors().get(word), c
									.getDictionaries(), word, m);
						}
						parser.move();
					}
				} catch (JSCerror e1) {
					e1.printStackTrace();
				}
			}
		});

		JButton loadDictionary = new JButton("Load Dictionary");
		loadDictionary.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FileChooserFrame(dictionaries);
			}
		});

		JButton createEmptyDictionary = new JButton("Create Dictionary");
		createEmptyDictionary.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = (String)JOptionPane.showInputDialog(new JDialog(),"Enter the name of the new dictionary.\n"+"(It will be stored in the \"dicts/\" directory.)","Create Dictionary",JOptionPane.QUESTION_MESSAGE);
				if(s!=null) {
					if(!s.endsWith(".jsc"))
						s+=".jsc";
					if(!s.startsWith("dicts/"))
						s="dicts/"+s;
					File f = new File(s);
					if (!f.exists()) {
						try {
							f.createNewFile();
						} catch (IOException g) {
							g.printStackTrace();
						}
					} else {
						JOptionPane.showMessageDialog(new JDialog(), "Failed at creating empty dictionary.");
					}
				}
			}
		});

		JButton quit = new JButton("Quit");
		quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quit();
			}
		});

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				quit();
			}
		});

		saisiePane.setLayout(layout);
		saisiePane.setPreferredSize(dimensions);
		this.setSize(dimensions);
		this.setLocationRelativeTo(null);
		saisiePane.add(textfield);
		saisiePane.add(checkSpell);
		saisiePane.add(loadDictionary);
		saisiePane.add(createEmptyDictionary);
		saisiePane.add(quit);

		this.pack();
		this.setVisible(true);
	}

	public void quit() {
		if (dictionaries.modified()) {
			Object[] options = { "Save", "Discard", "Cancel" };
			int n = JOptionPane
					.showOptionDialog(
							this,
							"A dictionary has been modified.\nDo you want to save your changes?",
							"Java Spell Checker",
							JOptionPane.YES_NO_CANCEL_OPTION,
							JOptionPane.QUESTION_MESSAGE, null, options,
							options[2]);
			switch (n) {
			case 0:
				dictionaries.allSave();
				System.exit(0);
				break;
			case 1:
				System.exit(0);
				break;
			}
		} else {
			System.exit(0);
		}
	}

	public void rewriteText(String error, String correction) {
		String text = textfield.getText();
		String newText = text.replaceFirst(error, correction);
		textfield.setText(newText);
	}

	public static void main(String[] args) {
		new MainFrame();
	}
}
